﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.RemoteMachine
{
    public class Hub : Node
    {
        public Hub() : this(false)
        {
        }

        public Hub(bool isSecure)
        {
            this.Features = new Dictionary<string, Network.Protocol.ProtocolFeature>();

            InitialiseFeatures(isSecure);

            mConnection = new HubConnection(this);
        }

        private void InitialiseFeatures(bool isSecure)
        {
            LocalMachine.Client cli = LocalMachine.Client.GetInstance();

            lock (cli)
            {
                // as with the same method in Peer.. wtf? Isn't this done in the SUP handler?
                string baseProtocolName = Network.Protocol.ProtocolFeature.GetFeatureName(typeof(Network.Protocol.BASE.ProtocolFeatureBASE));
                this.Features.Add(baseProtocolName, cli.Features[baseProtocolName]);
                if (isSecure)
                {
                    string adcsProtocolName = Network.Protocol.ProtocolFeature.GetFeatureName(typeof(Network.Protocol.ADCS.ProtocolFeatureADCS));
                    this.Features.Add(adcsProtocolName, cli.Features[adcsProtocolName]);
                }
            }
        }

        public override Connection Connection
        {
            get
            {
                return mConnection as HubConnection;
            }
            set
            {
                mConnection = value;
            }
        }

        public override System.Security.Cryptography.HashAlgorithm SessionHashAlgorithm
        {
            get
            {
                return base.SessionHashAlgorithm;
            }
            set
            {
                base.SessionHashAlgorithm = value;
                ShareManager.ShareManager sManager = ShareManager.ShareManager.GetInstance();
                lock (sManager)
                {
                    sManager.SetHashAlgorithm(value);
                }
            }
        }
    }
}
